Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make Django request span attributes available #1730

Conversation

rafa-munoz
Copy link
Contributor

@rafa-munoz rafa-munoz commented Mar 27, 2023

Description

I would like to be able not to instrument certain requests within Django. E.g. when HTTP http.method=OPTIONS.

To be able to do this, I'd need to be able to access request data through attributes, which at the moment is unused:

class DjangoSampler(Sampler):
    # ...
    def should_sample(
        self,
        parent_context,
        trace_id,
        name,
        kind=None,  # noqa
        attributes=None,
        links=None,  # noqa
        trace_state=None,
    ):
        decision = Decision.RECORD_AND_SAMPLE
        if attributes and attributes.get("http.method") == "OPTIONS":
            # Do not instrument OPTIONS requests
            decision = Decision.DROP
            attributes = None
        return SamplingResult(
            decision,
            attributes,
            self._get_parent_trace_state(parent_context),
        )

    resource = Resource(
        attributes={
            "service.name": "myapp",
        }
    )
    trace.set_tracer_provider(
        TracerProvider(
            sampler=DjangoSampler(),
            resource=resource,
        )
    )

Fixes partially #936

Type of change

Please delete options that are not relevant.

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

How Has This Been Tested?

Using the code in the description.

Does This PR Require a Core Repo Change?

  • Yes. - Link to PR:
  • No.

Checklist:

See contributing.md for styleguide, changelog guidelines, and more.

  • Followed the style guidelines of this project
  • Changelogs have been updated
  • Unit tests have been added
  • Documentation has been updated

@rafa-munoz rafa-munoz requested a review from a team March 27, 2023 13:43
@linux-foundation-easycla
Copy link

linux-foundation-easycla bot commented Mar 27, 2023

CLA Signed

The committers listed above are authorized under a signed CLA.

  • ✅ login: rafa-munoz / name: Rafael Muñoz (8c45979)

@rafa-munoz rafa-munoz force-pushed the django-attributes-available-in-sampler branch 2 times, most recently from 94e035b to 8c45979 Compare March 27, 2023 14:41
CHANGELOG.md Outdated Show resolved Hide resolved
@rafa-munoz rafa-munoz force-pushed the django-attributes-available-in-sampler branch from 6af495b to b9a8d76 Compare March 28, 2023 08:10
@rafa-munoz rafa-munoz changed the title Pass request attributes in Django Make Django request span attributes available Mar 28, 2023
@rafa-munoz rafa-munoz requested a review from srikanthccv March 28, 2023 08:11
@srikanthccv srikanthccv enabled auto-merge (squash) March 28, 2023 08:17
@rafa-munoz
Copy link
Contributor Author

@srikanthccv @lzchen for any reason the build is failing in Contrib Repo Tests / build (pypy3, instrumentation, ubuntu-20.04) (pull_request) but I think it's unrelated and the error may be gone by triggering the build again. I can't restart the build. Do you have permission for that?

@lzchen lzchen closed this Mar 28, 2023
auto-merge was automatically disabled March 28, 2023 19:40

Pull request was closed

@lzchen lzchen reopened this Mar 28, 2023
@lzchen lzchen closed this Mar 28, 2023
@lzchen lzchen reopened this Mar 28, 2023
@rafa-munoz
Copy link
Contributor Author

Still the same thing unfortunately:

pypy3-test-instrumentation-botocore run-test: commands[0] | pytest --benchmark-json=pypy3-instrumentation-ubuntu-20.04-benchmark.json
[863](https://github.com/open-telemetry/opentelemetry-python-contrib/actions/runs/4547788307/jobs/8018104485?pr=1730#step:6:864)
============================= test session starts ==============================
[864](https://github.com/open-telemetry/opentelemetry-python-contrib/actions/runs/4547788307/jobs/8018104485?pr=1730#step:6:865)
platform linux -- Python 3.7.13[pypy-7.3.9-final], pytest-7.2.2, pluggy-1.0.0 -- /home/runner/work/opentelemetry-python-contrib/opentelemetry-python-contrib/.tox/pypy3-test-instrumentation-botocore/bin/python
[865](https://github.com/open-telemetry/opentelemetry-python-contrib/actions/runs/4547788307/jobs/8018104485?pr=1730#step:6:866)
cachedir: .tox/pypy3-test-instrumentation-botocore/.pytest_cache
[866](https://github.com/open-telemetry/opentelemetry-python-contrib/actions/runs/4547788307/jobs/8018104485?pr=1730#step:6:867)
benchmark: 4.0.0 (defaults: timer=pytest_benchmark.timers.monotonic disable_gc=False min_rounds=5 min_time=0.000005 max_time=1.0 calibration_precision=10 warmup=True warmup_iterations=100000)
[867](https://github.com/open-telemetry/opentelemetry-python-contrib/actions/runs/4547788307/jobs/8018104485?pr=1730#step:6:868)
rootdir: /home/runner/work/opentelemetry-python-contrib/opentelemetry-python-contrib, configfile: pytest.ini
[868](https://github.com/open-telemetry/opentelemetry-python-contrib/actions/runs/4547788307/jobs/8018104485?pr=1730#step:6:869)
plugins: benchmark-4.0.0
[869](https://github.com/open-telemetry/opentelemetry-python-contrib/actions/runs/4547788307/jobs/8018104485?pr=1730#step:6:870)
collecting ... Fatal Python error: Aborted
[870](https://github.com/open-telemetry/opentelemetry-python-contrib/actions/runs/4547788307/jobs/8018104485?pr=1730#step:6:871)

[871](https://github.com/open-telemetry/opentelemetry-python-contrib/actions/runs/4547788307/jobs/8018104485?pr=1730#step:6:872)
Stack (most recent call first, approximate line numbers):
[872](https://github.com/open-telemetry/opentelemetry-python-contrib/actions/runs/4547788307/jobs/8018104485?pr=1730#step:6:873)
  File "<frozen importlib._bootstrap>", line 220 in _call_with_frames_removed
[873](https://github.com/open-telemetry/opentelemetry-python-contrib/actions/runs/4547788307/jobs/8018104485?pr=1730#step:6:874)
ERROR: InvocationError for command /home/runner/work/opentelemetry-python-contrib/opentelemetry-python-contrib/.tox/pypy3-test-instrumentation-botocore/bin/pytest --benchmark-json=pypy3-instrumentation-ubuntu-20.04-benchmark.json (exited with code -6 (SIGABRT)) (exited with code -6)

@srikanthccv
Copy link
Member

Please rebase. It should be fixed now.

@rafa-munoz rafa-munoz force-pushed the django-attributes-available-in-sampler branch from b9a8d76 to 04e5c54 Compare March 31, 2023 11:14
@srikanthccv srikanthccv merged commit 27d6706 into open-telemetry:main Mar 31, 2023
@rafa-munoz rafa-munoz deleted the django-attributes-available-in-sampler branch April 3, 2023 19:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants